package com.lanou.tipsy.adapter.collect;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.tipsy.R;

import com.lanou.tipsy.db.TipsyEntity;
import com.lanou.tipsy.db.TipsyEntityDao;
import com.lanou.tipsy.tools.volley.DaoSingleton;

import java.util.List;

/**
 * Created by wangshuai on 15/11/7.
 * 我的收藏适配器
 */
public class MyCollectAdapter extends BaseAdapter {
    public static final String TAG = "MyCollectAdapter";
    private Context context;
    /**
     * 通过单例模式进行数据的内容查询,数据库的单例:DaoSingleton
     */
    private TipsyEntityDao tipsyEntityDao;
    private List<TipsyEntity> tipsyEntities;

    public MyCollectAdapter(Context context) {
        this.context = context;
        tipsyEntityDao = DaoSingleton.getInstance().getTipsyEntityDao();
        tipsyEntities = tipsyEntityDao.loadAll();
        Log.i("test", TAG +   tipsyEntities.size());
        Log.i("test", TAG  +  tipsyEntityDao);
    }


    @Override
    public int getCount() {
        return tipsyEntities != null && tipsyEntities.size() > 0 ? tipsyEntities.size() : 0;
    }

    @Override
    public TipsyEntity getItem(int position) {
        return tipsyEntities != null && tipsyEntities.size() > 0 ? tipsyEntities.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_collect_music_list, null);
            holder = new ViewHolder();
            holder.collect_music_title = (TextView) convertView.findViewById(R.id.collect_music_title);
            holder.collect_music_artist = (TextView) convertView.findViewById(R.id.collect_music_artist);
            holder.collect_music_duration = (TextView) convertView.findViewById(R.id.collect_music_duration);
            holder.tv_collect_position = (TextView) convertView.findViewById(R.id.tv_collect_position);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        TipsyEntity te = getItem(position);
        if (te != null) {
            int p = position + 1 ;
            holder.collect_music_artist.setText(te.getSingerName());
            holder.collect_music_title.setText(te.getSongTitle());
            holder.collect_music_duration.setText(te.getTag());
            holder.tv_collect_position.setText(p + "");
        }

        return convertView;
    }

    /**
     * 用于缓存行布局中需要概念内容的个别组件
     * 很重要,不能没有
     */
    private class ViewHolder {
        private TextView collect_music_title;
        private TextView collect_music_artist;
        private TextView collect_music_duration;
        private TextView tv_collect_position;

    }

}
